import { TOUCH_MOVE_LEN } from "../../base/GameIndex";
import { Tools } from "../../common/Tools";
import TouchScale from "../../item/Common/TouchScale";
import LevelPre_2 from "./LevelPre_2";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LvItem_2 extends cc.Component {
    /** 是否可点击  Bool */
    isClick: boolean = false;
    /** 是否电机正确 */
    isKey: boolean = false;
    /** 配置信息 */
    config: any = null;
    /** 关卡总类 */
    levelBase: LevelPre_2 = null;
    // onLoad () {}
    // start () {
    // }
    touchStartPos: cc.Vec3 = null;
    isTouchMove: boolean = false;
    initItem(config: any, levelBase: LevelPre_2) {
        this.config = config;
        this.levelBase = levelBase;

        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartBack, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchMoveEndBack, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchMoveEndBack, this);

        this.isClick = true;
        this.node.addComponent(cc.Button);
    };
    /** 触摸开始回调 */
    touchStartBack(touches: cc.Event.EventTouch) {
        if (!this.isClick) { return; }
        this.touchStartPos = cc.v3(touches.getLocation());
        this.isTouchMove = false;
    };
    /** 触摸移动回调 */
    touchMoveBack(touches: cc.Event.EventTouch) {
        if (!this.touchStartPos || Tools.getDistance(this.touchStartPos, touches.getLocation()) <= TOUCH_MOVE_LEN ) { return; }
        this.isTouchMove = true;
        this.levelBase.lvNode.getComponent(TouchScale).touchMoveCall(touches);
    };
    /** 触摸移动结束回调 */
    touchMoveEndBack(touches: cc.Event.EventTouch) {
        if (!this.isClick) { return; }
        if (!this.isTouchMove) {
            this.clickItem();
        }
    };
    /** 点击按钮 */
    clickItem() {
        if (!this.isClick || this.isKey) { return; }
        this.isClick = false;
        this.isKey = true;

        this.levelBase.keyCorret(this);
        this.showYuanNode(true);
        this.playAudio();

        let tw = this.levelBase.getInterTwForName(this.node.name,"click");
        if(tw){
            this.levelBase.runNodeTween(this.node,tw.interTween);
        }
        if( this.config.isReturn ){
            this.runReturnContent();
        }
    };
    lvYuan: cc.Node = null;
    hongYuan:cc.Node = null;
    /** 显示圆圈 */
    showYuanNode(isClick: boolean = false) {
        if( !this.node.active ){ return; }
        if( this.hongYuan ){ return; }
        // cc.log(isClick);
        let yuanNode:cc.Node = null;
        if (isClick == false) {
            yuanNode = Tools.newPrefab("yuan_Green");
            this.lvYuan = yuanNode;
            this.setRot(this.lvYuan);
        }else {
            if (this.lvYuan) {
                this.lvYuan.destroy();
                this.lvYuan = null;
            }
            yuanNode = Tools.newPrefab("yuan_Red");
            this.hongYuan = yuanNode;
            this.setRot(this.hongYuan);
        }
        this.node.parent.addChild(yuanNode);

        if( this.config.yuanScale ){
            yuanNode.setScale(this.config.yuanScale.x, this.config.yuanScale.y);
        }else{
            yuanNode.setContentSize( this.node.width , this.node.height );
        }
        if( this.levelBase.interJson.People){
            yuanNode.opacity = 0;
        }
        yuanNode.position = this.node.position;
    };
    /** 隐藏圈 */
    hideYuan(){
        if(this.lvYuan){
            this.lvYuan.active = false;
        }
        if(this.hongYuan){
            this.hongYuan.active = false;
        }
    };
    setRot(node:any){
        if(this.config.yuanRotation != null)
        {
            node.angle = this.config.yuanRotation;
        }
    }

    /** 播放声音 */
    playAudio() {
        if (!this.config.audio) { 
            this.levelBase.playAudio( { resUrl:"xuandui" , value: 1 } );
            return; 
        }
        this.levelBase.playAudio(this.config.audio);
    };
    protected onEnable(): void {
        if(!this.levelBase){
            return;
        }
        if( !this.lvYuan && !this.hongYuan && this.levelBase.isAddKey ){
            this.showYuanNode();
        }
    }
    /** 执行本节点返回道具栏 */
    runReturnContent() {
        let downItem = this.levelBase.downContent.children[this.levelBase.finishItemArr.length - 1];
        this.node.children[0].active = true;
        let node = cc.instantiate( this.node.children[0] );
        node.parent = this.node;

        node.scale = node.scale * this.levelBase.lvNode.scale;
        let toPos = Tools.getToNodePosForNode(downItem, node.parent );
        let tempWidth = Math.max(node.width, node.height);
        let toScale = 90 / tempWidth;
        cc.tween(node)
        .to(0.05, { scale: node.scale + 0.2 })
        .to(0.15, { position: cc.v3(toPos), scale: toScale })
        .call( () => {
            Tools.setNodeParent( node , downItem );
        } )
        .to(0.05, { position: cc.v3(0,0,0) })
        .start();
    };
}
